import { _decorator, Component } from 'cc';
import { BaseSkill } from '../skills/BaseSkill';
import { EventManager } from '../core/EventManager';
import { GameEvents } from '../constants/GameConstants';

const { ccclass, property } = _decorator;

/**
 * 技能系统
 * 
 * 功能：
 * 1. 管理角色技能
 * 2. 处理技能解锁和升级
 * 3. 控制技能冷却和消耗
 * 4. 处理技能效果
 * 
 * 特点：
 * 1. 技能组合系统
 * 2. 技能等级管理
 * 3. 技能效果配置
 * 4. 技能触发条件
 */
@ccclass('SkillSystem')
export class SkillSystem extends Component {
    @property([BaseSkill])
    private skills: BaseSkill[] = [];

    private activeSkills: Map<string, BaseSkill> = new Map();

    start() {
        // 监听技能解锁事件
        EventManager.instance.on(GameEvents.SKILL_UNLOCKED, this.onSkillUnlocked, this);
    }

    private onSkillUnlocked(skillId: string) {
        // 激活对应的技能组件
        const skill = this.skills.find(s => s.node.name === skillId);
        if (skill) {
            skill.enabled = true;
            this.activeSkills.set(skillId, skill);
        }
    }

    public useSkill(skillId: string): boolean {
        const skill = this.activeSkills.get(skillId);
        if (!skill) return false;

        return skill.use();
    }

    public getSkillCooldown(skillId: string): number {
        const skill = this.activeSkills.get(skillId);
        if (!skill) return 0;

        return skill.getCooldownProgress();
    }

    onDestroy() {
        EventManager.instance.off(GameEvents.SKILL_UNLOCKED, this.onSkillUnlocked, this);
    }
} 